<?php
declare(strict_types=1);

namespace App\Controller\v1;

use Throwable;
use App\Helper\Log;
use App\Helper\Util;
use App\Helper\Common;
use Hyperf\DbConnection\Db;
use App\Constants\ReturnCode;
use Hyperf\Di\Annotation\Inject;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Service\UserService;
use App\Middleware\Api\AuthMiddleware;
use App\Service\PushService;

#[Controller]
#[Middleware(AuthMiddleware::class)]
class UserController extends AbstractController
{
    #[Inject]
    protected UserService $userServ;
    #[Inject]
    protected PushService $pushServ;

    #用户微信公众号关注状态
    #[RequestMapping(path: "wxsubstatus", methods: "post")]
    public function wxsubstatus(){
        $user_id = $this->request->getAttribute('user_id');
        $this->userServ->setUserId($user_id);
        $res = $this->userServ->checkSubStatus();
        return $this->fmt_response(ReturnCode::deal_succ, '获取成功', $res);
    }
    #个人信息
    #[RequestMapping(path: "info", methods: "post")]
    public function info()
    {
        $user_id = $this->request->getAttribute('user_id');
        $this->userServ->setUserId($user_id);
        $res = $this->userServ->getUserInfo();
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $res);
    }
    #个人信息保存
    #[RequestMapping(path: "save-info", methods: "post")]
    public function saveInfo()
    {
        $avatar = $this->request->input("avatar", "");
        $nickname = $this->request->input("nickname", "");
        $user_id = $this->request->getAttribute('user_id');
        $params = [
            'avatar'=>$avatar,
            'nickname'=>$nickname
        ];
        $this->userServ->setUserId($user_id);
        $res = $this->userServ->saveUserInfo($params);
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
    #注销
    #[RequestMapping(path: "logoff", methods: "post")]
    public function logoff()
    {
        $user_id = $this->request->getAttribute('user_id');
        $this->userServ->setUserId($user_id);
        $res = $this->userServ->logoff();
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
    #关于我们
    #[RequestMapping(path: "aboutus", methods: "post")]
    public function aboutus()
    {
        $user_id = $this->request->getAttribute('user_id');
        $this->userServ->setUserId($user_id);
        $res = $this->userServ->aboutus();
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $res);
    }
    #推送设置详情
    #[RequestMapping(path: "setpush-detail", methods: "post")]
    public function setpushDetail()
    {
        $user_id = $this->request->getAttribute('user_id');
        $res = $this->pushServ->getSettings($user_id);
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $res);
    }
    #推送设置
    #[RequestMapping(path: "setpush", methods: "post")]
    public function setpush()
    {
        $type = $this->request->input("type", "");
        $value = $this->request->input("value", -1);
        if(empty($type)){
            return $this->fmt_response(ReturnCode::para_miss,'类型错误');
        }
        if($value==-1){
            return $this->fmt_response(ReturnCode::para_miss,'值错误');
        }
        $user_id = $this->request->getAttribute('user_id');
        $res = $this->pushServ->putSettings($type, $value, $user_id);
        if($value==1){
            $res = "已开启";
        }elseif($value==0){
            $res = "已关闭";
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $res);
    }
}